<template>
    <div>
        <div>
            <el-col :span="3">
                <div class="grid-content bg-purple"></div>
                <el-button type="primary" @click="onhandAdd">添加衣物种类</el-button>
            </el-col>
            <el-input style="width: 240px;" placeholder="请输入衣物种类" v-model="params.clothesKindName"></el-input>
            <el-button style="margin-left: 10px;" type="primary" icon="el-icon-search " @click="loads">搜索</el-button>
            <el-button style="margin-left: 10px;" type="warning" icon="el-icon-scissors " @click="reset">重置</el-button>
        </div>
        <br>
        <div>
            <el-col :span="6">
                <div class="grid-content bg-purple"></div>
                <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                               title="确认删除所有选中的衣物种类吗？" @confirm="deleteSelected">
                    <el-button type="danger" slot="reference"  v-bind:disabled="disable">删除所选</el-button>
                </el-popconfirm>
            </el-col>
        </div>
        <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%" tooltip-effect="dark"
                  @selection-change="handleSelectionChange">
            <el-table-column
                    prop="clothesKindId"
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="clothesKindName" label="衣物种类名称" width="180">
            </el-table-column>
            <el-table-column prop="clothesPrice" label="价格" width="180">
            </el-table-column>
            <el-table-column label="操作" prop="clothesKindId">
                <template v-slot="scope">
                    <el-button type="primary" @click="handleEdit(scope.row.clothesKindId)">编辑
                    </el-button>
                    <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                                   title="确认删除此衣物种类吗？" @confirm="del(scope.row.clothesKindId)">
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
            <el-button @click="toggleSelection()">取消选择</el-button>
        </div>
        <!-- 分页 -->
        <div style="margin-top: 10px;">
            <el-pagination background :current-page="params.pageNum" :page-size="params.pageSize"
                           layout="prev, pager, next" :total="total" @current-change="handleCurrentChange">
            </el-pagination>
        </div>
        <!-- 新增编辑弹框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :inline="true" :model="clotheskindform" status-icon :rules="rules" ref="clotheskindform"
                     style="width: 70%;"
                     label-width="180px">
                <el-form-item label="衣物种类名称" prop="clothesKindName" v-show="nameshow">
                    <el-input v-model="clotheskindform.clothesKindName" placeholder="请输入会员种类名称"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="clothesPrice" >
                    <el-input v-model="clotheskindform.clothesPrice" placeholder="请输入价格"></el-input>
                </el-form-item>

            </el-form>
            <div style="text-align-last: center;width: 60%;">
                <el-button @click="closedialog('clotheskindform')" type="danger">取 消</el-button>
                <el-button @click="resetForm('clotheskindform')" type="warning">重置</el-button>
                <el-button style="margin-right: 30px;" type="primary" icon="el-icon-upload2 "
                           @click="dialogVisibleConfirm('clotheskindform')" size="medium">添加
                </el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from '@/request'

    export default {
        name: 'ClothesKind',
        data() {
            return {
                //选中的id存全局
                multipleSelection: [],
                multiple: [],
                tableData: [],
                total: 0,
                allid: "",
                title: "",
                disable:true,
                dialogVisible: false,
                nameshow:true,
                params: {
                    pageNum: 1,
                    pageSize: 8
                },
                clotheskindform: {
                    clothesKindName: '',
                    clothesPrice: ''
                },

                rules: {
                    clothesKindName: [{
                        required: true,
                        message: '衣物种类名称不能为空',
                        trigger: 'blur'
                    },],
                    clothesPrice: [{
                        required: true,
                        message: '价格不能为空',
                        trigger: 'blur'
                    },],

                }
            }
        },
        mounted() {
            this.load()
        },
        methods: {
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            //多选框被更改的操作
            handleSelectionChange(val) {
                //如果什么都没有选中则禁用删除按钮
                if (val.length==0){
                    this.multipleSelection.length=val.length
                    this.disable=true
                }else {
                    this.disable=false
                    //将选中的id存放全局
                    this.multipleSelection.length=val.length
                    for (let i = 0; i < val.length ;i++) {
                        this.multipleSelection[i]=val[i].clothesKindId
                    }
                 }
            },
            add(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        request.post('/clotheskind/add', this.clotheskindform).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('新增成功')
                                this.resetForm(formName) //清空表格数据
                                this.dialogVisible = false; //关闭弹框
                                this.load(); //刷新列表
                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                });
            },
            update(formName) {
                request.put('/clotheskind/update', this.clotheskindform).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('修改成功')
                        this.resetForm(formName) //清空表格数据
                        this.dialogVisible = false; //关闭弹框
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            del(clothesKindId) {
                request.delete('/clotheskind/delete' + clothesKindId).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('删除成功')
                        this.load();
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            deleteSelected(){
                console.log(this.multipleSelection)
                request.post('/clotheskind/deletes', this.multipleSelection).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('批量删除成功')
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            handleClose(done) {
                this.dialogVisible = false;
            },
            onhandAdd() {
                this.nameshow=true
                this.title = '添加衣物种类';
                this.resetForm('clotheskindform');
                this.dialogVisible = true;
            },
            handleEdit(clothesKindId) {
                this.nameshow=false
                this.title = '修改衣物种类信息'
                this.dialogVisible = true; //显示弹框
                request.get("/clotheskind/" + clothesKindId).then(res => {
                    this.clotheskindform = res.data
                })//row当前行数据，把当前行的数据赋值给 表单
                this.allid = clothesKindId; //把id存全局
            },

            //清空表单
            resetForm(formName) {
                this.$nextTick(() => {
                    this.$refs[formName].resetFields();
                })
            },
            //页面加载
            loads() {
                this.params.pageNum = 1
                this.load()
            },
            load() {
                request.get('/clotheskind/page', {
                    params: this.params
                }).then(res => {
                    console.log(res)
                    if (res.code === "200") {
                        this.total = res.data.total;
                        this.tableData = res.data.list;
                    }
                })
            },
            reset() {
                this.params = {
                    user_name: '',
                    user_num: '',
                    pageNum: 1,
                    pageSize: 10
                }
                this.load()
            },

            //核心判断哪一个按钮调用表单组件
            dialogVisibleConfirm(formName) {
                // 新增接口
                if (!this.allid) {
                    this.add(formName)

                } else {
                    // 修改接口
                    this.update(formName);
                }
            },

            closedialog(formName) {
                this.dialogVisible = false;
                this.resetForm(formName) //清空表格数据
            },

            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum;
                this.load()
            }
        }
    }
</script>
